package com.ls.server.controller;

import com.ls.server.dto.DictItemDTO;
import com.ls.server.dto.DictQueryDTO;
import com.ls.server.service.DictItemService;
import com.ls.server.vo.DictItemVO;
import com.ls.server.vo.PageResult;
import com.ls.server.vo.Result;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典小类控制器
 */
@RestController
@RequestMapping("/dict/item")
public class DictItemController {

    private final DictItemService dictItemService;

    public DictItemController(DictItemService dictItemService) {
        this.dictItemService = dictItemService;
    }

    /**
     * 分页查询字典项
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @PostMapping("/page")
    public Result<PageResult<DictItemVO>> page(@RequestBody DictQueryDTO queryDTO) {
        return Result.success(dictItemService.page(queryDTO));
    }

    /**
     * 根据字典大类ID查询字典项列表
     * @param categoryId 字典大类ID
     * @return 字典项列表
     */
    @GetMapping("/list/{categoryId}")
    public Result<List<DictItemVO>> listByCategoryId(@PathVariable Long categoryId) {
        return Result.success(dictItemService.listByCategoryId(categoryId));
    }

    /**
     * 根据ID查询字典项
     * @param id 字典项ID
     * @return 字典项
     */
    @GetMapping("/{id}")
    public Result<DictItemVO> getById(@PathVariable Long id) {
        return Result.success(dictItemService.getById(id));
    }

    /**
     * 新增字典项
     * @param dto 字典项信息
     * @return 操作结果
     */
    @PostMapping
    public Result<Boolean> save(@RequestBody DictItemDTO dto) {
        return Result.success(dictItemService.save(dto));
    }

    /**
     * 修改字典项
     * @param dto 字典项信息
     * @return 操作结果
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody DictItemDTO dto) {
        return Result.success(dictItemService.update(dto));
    }

    /**
     * 删除字典项
     * @param id 字典项ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(dictItemService.delete(id));
    }
} 